﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Data;

namespace TouchUmlUI
{

    public enum TabState { SELECTED, NOT_SELECTED, NOT_SELECTED_HOVER, NONE, HIDDEN }

    public class OurTab : Canvas
    {
        private String title;
        private TabState state;
        private static BitmapImage leftSelectedSource;
        private static BitmapImage rightSelectedSource;        
        private static BitmapImage leftNotSelectedSource;
        private static BitmapImage rightNotSelectedSource;
        private Image leftImage;
        private Image rightImage;
        private Image leftOffImage;
        private Image rightOffImage;
        private OurTabbedConrol tabbedControlParent;
        private HoverButton closeButton;
        private int tabId;
        public static readonly DependencyProperty ContentWidthProperty;        
        public InkCanvas canvas;
        public int contentWidth
        {
            get { return (int)this.GetValue(ContentWidthProperty); }
            set { this.SetValue(ContentWidthProperty, value); }
        }
        private Canvas c = new Canvas();

        public int getTabId()
        {
            return tabId;
        }

        public OurTabbedConrol getTabbedControl()
        {
            return tabbedControlParent;
        }

        public void setTabbedControl(OurTabbedConrol otc) 
        {
            tabbedControlParent = otc;                                                
        }

        public static void contentWidthChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            //System.Console.WriteLine("DependencyObject: " + o);
            OurTab kObj = (OurTab)o;
            kObj.tabbedControlParent.rearrangeTabs();
            kObj.Rearrange();
            //Canvas.SetLeft(leftImage, 0); Canvas.SetTop(leftImage, 0);            
            //Canvas.SetLeft(rightImage, (int)kObj.GetValue(OurTab.ContentWidthProperty) - 18); Canvas.SetTop(rightImage, 0);
        }

        static OurTab()
        {
            OurTab.ContentWidthProperty = DependencyProperty.Register("ContentWidthProperty", typeof(int), typeof(OurTab), new PropertyMetadata(new PropertyChangedCallback(contentWidthChanged)));
            leftSelectedSource = new BitmapImage();
            rightSelectedSource = new BitmapImage();
            leftNotSelectedSource = new BitmapImage();
            rightNotSelectedSource = new BitmapImage();
            // -----
            leftSelectedSource.BeginInit();
            leftSelectedSource.UriSource = new Uri("ikony\\left.png", UriKind.Relative);
            leftSelectedSource.EndInit();

            rightSelectedSource.BeginInit();
            rightSelectedSource.UriSource = new Uri("ikony\\right.png", UriKind.Relative);
            rightSelectedSource.EndInit();

            leftNotSelectedSource.BeginInit();
            leftNotSelectedSource.UriSource = new Uri("ikony\\leftoff.png", UriKind.Relative);
            leftNotSelectedSource.EndInit();

            rightNotSelectedSource.BeginInit();
            rightNotSelectedSource.UriSource = new Uri("ikony\\rightoff.png", UriKind.Relative);
            rightNotSelectedSource.EndInit();
        }

        public void SetId(int an)
        {
            tabId = an;
        }

        public void Rearrange()
        {
            closeButton.Visibility = tabbedControlParent.getVisibleTabsCount() == 1 ? Visibility.Hidden : Visibility.Visible;
            int nWidth = (int)this.GetValue(OurTab.ContentWidthProperty);
            //System.Console.WriteLine("OurTabRearrange: " + (int)this.GetValue(OurTab.ContentWidthProperty));
            Canvas.SetLeft(leftImage, 0); Canvas.SetTop(leftImage, 0);
            Canvas.SetLeft(c, 18);
            c.Width = nWidth + 10;
            Canvas.SetLeft(rightImage, (int)this.GetValue(OurTab.ContentWidthProperty) + 28); Canvas.SetTop(rightImage, 0);
            Canvas.SetLeft(rightOffImage, (int)this.GetValue(OurTab.ContentWidthProperty) + 28); Canvas.SetTop(rightOffImage, 0);
            Canvas.SetLeft(closeButton, (int)this.GetValue(OurTab.ContentWidthProperty) + 18); Canvas.SetTop(closeButton, 3);

        }

        public void Refresh()
        {
            if (state == TabState.HIDDEN) return;
            this.Children.Clear();
            closeButton = null;
            Width = 100;
            Color textColor = new Color();          
           // contentWidth = newWidth;
            if (state == TabState.SELECTED)
            {
                closeButton = new HoverButton("ikony\\icon_tabonclose.png", "ikony\\icon_tabonclosehover.png", 10, 10);
                textColor = Color.FromArgb(255, 221, 134, 49);
                Children.Add(leftImage);                
                c.Background = Brushes.White;
                //c.Width = newWidth - 36;
                c.MouseDown += imageMouseDown;                
                c.Height = 18;
                Children.Add(c);
                Children.Add(rightImage);
                Children.Add(closeButton);                
                this.Opacity = 1.0f;
            }
            else if (state == TabState.NOT_SELECTED)
            {
                closeButton = new HoverButton("ikony\\icon_taboffclose.png", "ikony\\icon_taboffclosehover.png", 10, 10);                
                textColor = Color.FromArgb(255, 255, 255, 255);
                Children.Add(leftOffImage);               
                c.Background = new SolidColorBrush(Color.FromArgb(255,176, 171, 209));
                //c.Width = newWidth - 36;
                c.Height = 18;
                c.MouseDown += imageMouseDown;
                c.MouseEnter += imageOffMouseEnter;
                c.MouseLeave += imageOffMouseLeave;
                Children.Add(c);
                Children.Add(rightOffImage);
                Children.Add(closeButton);
                closeButton.MouseEnter += imageOffMouseEnter;
                closeButton.MouseLeave += imageOffMouseLeave;                
            }
            closeButton.Tag = this;
            closeButton.MouseDown += tabbedControlParent.deleteTabEvent;            
            Label l = new Label();
            l.Content = getTitle();
            l.FontSize = 11;
            l.FontFamily = new FontFamily("Tahoma");
            l.Foreground = new SolidColorBrush(textColor);            
            Canvas.SetLeft(l, 19);
            Canvas.SetTop(l, -2);
            l.MouseDown += imageMouseDown;
            if (state != TabState.SELECTED)
            {
                l.MouseEnter += imageOffMouseEnter;
                l.MouseLeave += imageOffMouseLeave;
            }

            Binding bind1 = new Binding("ActualWidthProperty");
            bind1.Source = l;
            bind1.Path = new PropertyPath(Label.ActualWidthProperty);
            this.SetBinding(OurTab.ContentWidthProperty, bind1);
            Binding bind2 = new Binding("ActualWidthProperty");
            bind2.Source = l;
            bind2.Path = new PropertyPath(Label.ActualWidthProperty);
            c.SetBinding(Canvas.WidthProperty, bind2);
            Children.Add(l);
        }

        public void imageMouseDown(object sender, MouseButtonEventArgs e)
        {                        
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                tabbedControlParent.Select(tabId);
                tabbedControlParent.Refresh();
            }
        }

        public void imageOffMouseEnter(object sender, MouseEventArgs e)
        {            
            this.Opacity = 1.0f;
        }

        public void imageOffMouseLeave(object sender, MouseEventArgs e)
        {            
            if (state != TabState.SELECTED)
            this.Opacity = 0.7f;
        }


        public OurTab(String a_title, InkCanvas aCanvas,OurTabbedConrol otc)
        {
            canvas = aCanvas;
            state = TabState.NOT_SELECTED;
            title = a_title;
            leftImage = new Image();
            rightImage = new Image();
            leftOffImage = new Image();
            rightOffImage = new Image();
            leftImage.Source = leftSelectedSource;
            rightImage.Source = rightSelectedSource;
            leftOffImage.Source = leftNotSelectedSource;
            rightOffImage.Source = rightNotSelectedSource;
            leftImage.Width = leftImage.Height = rightImage.Width = rightImage.Height = leftOffImage.Width = leftOffImage.Height = rightOffImage.Width = rightOffImage.Height = 18;
            leftImage.Stretch = rightImage.Stretch = leftOffImage.Stretch = rightOffImage.Stretch = System.Windows.Media.Stretch.Fill;
            //---            
            leftImage.MouseDown += imageMouseDown;
            rightImage.MouseDown += imageMouseDown;
            leftOffImage.MouseDown += imageMouseDown;
            rightOffImage.MouseDown += imageMouseDown;
            //---
            leftOffImage.MouseEnter += imageOffMouseEnter;
            rightOffImage.MouseEnter += imageOffMouseEnter;
            leftOffImage.MouseLeave += imageOffMouseLeave;
            rightOffImage.MouseLeave += imageOffMouseLeave;
            this.Opacity = 0.8;
            //---
            this.MouseDown += otc.tabMouseDown;
            this.MouseUp += otc.tabMouseUp;
            this.MouseMove += otc.mouseMove;
        }

        public String getTitle()
        {
            return title;
        }

        public void setTitle(String a) 
        {
            title = a;
        }

        public TabState getTabState()
        {
            return state;
        }

        public void setTabState(TabState a)
        {
            state = a;
        }


        public void updateElementPositions()
        {
            foreach (UMLObj uObj in ((OurCanvas)canvas).al)
            {
                if (uObj.guml.obj_uml is Diagram.ElementNaDiagramie)
                {
                    ((Diagram.ElementNaDiagramie)uObj.guml.obj_uml).polozenie.X = (int)uObj.getX();
                    ((Diagram.ElementNaDiagramie)uObj.guml.obj_uml).polozenie.Y = (int)uObj.getY();
                    ((Diagram.ElementNaDiagramie)uObj.guml.obj_uml).rozmiar.Width = (int)uObj.getWidth();
                    ((Diagram.ElementNaDiagramie)uObj.guml.obj_uml).rozmiar.Height = (int)uObj.getHeight();
                }
            }
        }
    }
}
